package com.flame.ai.pdf.bean;

import com.flame.ai.pdf.PdfDocumentReader;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jflame
 * @version 1.0.0
 * @since 2025/8/27
 */
@Getter
public class DocumentItem {
    private Map<String, Object> metadata = new HashMap<>();

    public void setPage(int page) {
        if (metadata == null) {
            return;
        }

        List<Integer> pages = null;
        Object pagesObj = metadata.get(PdfDocumentReader.METADATA_PAGE_NUMBER);

        if (pagesObj instanceof List) {
            pages = (List<Integer>) pagesObj;
        }

        if (pages == null) {
            pages = new ArrayList<>();
        } else {
            // 创建新的ArrayList以避免修改不可变集合
            pages = new ArrayList<>(pages);
        }

        page++;
        if (pages.contains(page)) {
            return;
        }
        pages.add(page);
        metadata.put(PdfDocumentReader.METADATA_PAGE_NUMBER, pages);
    }

    public void setType(String type) {
        if (metadata == null) {
            return;
        }
        metadata.put(PdfDocumentReader.METADATA_DATA_TYPE, type);
    }
}
